package com.huawen.cms.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huawen.cms.app.entity.ArticlePart;
import com.huawen.cms.app.service.ArticlePartService;
import com.huawen.cms.app.util.CommonResult;
import com.huawen.cms.app.util.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/articlePart")
@RestController
public class ArticlePartController extends BaseController {

    @Autowired
    private ArticlePartService articlePartService;

    @RequestMapping(value = "/getById", method = RequestMethod.GET)
    public ArticlePart getById(Long id) {
        ArticlePart articlePart = (ArticlePart) articlePartService.getById(id);
        return articlePart;
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public ArticlePart insert(@RequestBody ArticlePart articlePart) {
        articlePartService.insert(articlePart);
        return articlePart;
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ArticlePart update(@RequestBody ArticlePart articlePart) {
        articlePartService.update(articlePart);
        return articlePart;
    }

    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    public List<ArticlePart> getList(@RequestBody ArticlePart articlePart) {
        List<ArticlePart> list = articlePartService.getList(articlePart);
        return list;
    }

    @RequestMapping(value = "/getPagerList", method = RequestMethod.POST)
    public PageInfo<ArticlePart> getPagerList(@RequestBody ArticlePart articlePart) {
        Pager pager = getCurrentPager();
        PageHelper.startPage(pager.getPageNum(), pager.getPageSize());
        List<ArticlePart> list = articlePartService.getList(articlePart);
        PageInfo<ArticlePart> pageInfo = new PageInfo<ArticlePart>(list);
        return pageInfo;
    }

    @RequestMapping(value = "/ArtPartEdit", method = RequestMethod.POST)
    public CommonResult ArtPartEdit(@RequestBody List<ArticlePart> list, String toDelIds) {

        for (ArticlePart part : list) {
            if (part.getId() > 0) {
                articlePartService.update(part);
            } else {
                articlePartService.insert(part);
            }
        }

//        String[] idsArr = toDelIds.split(",");
//        for (String id : idsArr) {
//            ArticlePart mod = (ArticlePart) articlePartService.getById(Integer.parseInt(id));
//            mod.setDataFlag(1);
//            articlePartService.update(mod);
//        }

        return new CommonResult();
    }

}
